package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;


/**
 * ShoppingCartServiceImpl
 *
 * @author liliudong
 * @version 1.0
 * @description
 * @date 2023/8/2 14:07
 */
@Service
@RequiredArgsConstructor
public class ShoppingCartServiceImpl implements ShoppingCartService {

    private final ShoppingCartMapper shoppingCartMapper;

    private final DishMapper dishMapper;

    private final SetmealMapper setmealMapper;

    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        if (Objects.nonNull(shoppingCartDTO.getDishId())) {
            // 1. 菜品 这个菜品是否已经存在购物车里
            // 菜品的唯一性：谁，dishId,口味
            ShoppingCart cartQuery = new ShoppingCart();
            BeanUtils.copyProperties(shoppingCartDTO, cartQuery);
            cartQuery.setUserId(BaseContext.getCurrentId());
            ShoppingCart shoppingCart = shoppingCartMapper.selectOne(cartQuery);
            // 2. 如果存在，进行数量 + 1
            Dish dish = dishMapper.selectById(shoppingCartDTO.getDishId());
            if (Objects.nonNull(shoppingCart)) {
                Integer number = shoppingCart.getNumber();
                shoppingCart.setNumber(number + 1);
                // 总金额 = 数量 * 单价
                shoppingCart.setAmount(new BigDecimal(shoppingCart.getNumber()).multiply(dish.getPrice()));
                shoppingCartMapper.updateById(shoppingCart);
            } else {
                // 3. 如果不存在，新增一条数据，数量默认 1
                shoppingCart = new ShoppingCart();
                // 存 取不到
                shoppingCart.setUserId(BaseContext.getCurrentId());
                shoppingCart.setDishId(shoppingCartDTO.getDishId());
                shoppingCart.setDishFlavor(shoppingCartDTO.getDishFlavor());
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());
                // 4. 处理冗余字段 - 查询菜品的信息
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());
                shoppingCart.setImage(dish.getImage());
                // 保存
                shoppingCartMapper.insert(shoppingCart);
            }

        }

        if (Objects.nonNull(shoppingCartDTO.getSetmealId())) {
            // 1. 套餐 这个套餐是否已经存在购物车里
            ShoppingCart cartQuery = new ShoppingCart();
            cartQuery.setUserId(BaseContext.getCurrentId());
            cartQuery.setSetmealId(shoppingCartDTO.getSetmealId());
            ShoppingCart shoppingCart = shoppingCartMapper.selectOne(cartQuery);
            // 2. 如果存在，进行数量 + 1
            Setmeal setmeal = setmealMapper.selectById(shoppingCartDTO.getSetmealId());
            if (Objects.nonNull(shoppingCart)) {
                Integer number = shoppingCart.getNumber();
                shoppingCart.setNumber(number + 1);
                shoppingCart.setAmount(new BigDecimal(shoppingCart.getNumber()).multiply(setmeal.getPrice()));
                shoppingCartMapper.updateById(shoppingCart);
            } else {
                // 3. 如果不存在，新增一条数据，数量默认 1
                shoppingCart = new ShoppingCart();
                shoppingCart.setUserId(BaseContext.getCurrentId());
                shoppingCart.setSetmealId(shoppingCartDTO.getSetmealId());
                shoppingCart.setNumber(1);
                shoppingCart.setCreateTime(LocalDateTime.now());
                // 4. 处理冗余字段
                shoppingCart.setAmount(setmeal.getPrice());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setName(setmeal.getName());
                shoppingCartMapper.insert(shoppingCart);
            }
        }
    }

    @Override
    public List<ShoppingCart> list() {
        // 谁的数据
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        return shoppingCartMapper.selectList(shoppingCart);
    }

    @Override
    public void clean() {
        shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }
}
